<?php

class page_obj{
	public $current_page;	/*当前页*/
	public $border_length;	/*当前两边有几个数字*/
	public $length_page;	/*总共有多少页*/
	public $arr_temp;		/*临时变量*/
	public $perpageItemNum;	/*每页有输出记录数*/
	public $totalItemNum;	/*记录总数*/	
	public $page_template;	/*分页*/
	public $tablename;
	public $where;
	public $row_count=false;
	function page_obj($argcurrent,$arr_temp=""){		
		$this->current_page=$argcurrent?$argcurrent:1;
		$this->perpageItemNum=$arr_temp["itemnum"]?$arr_temp["itemnum"]:2;
		$this->border_length=$arr_temp["border"]?$arr_temp["border"]:2;	
		$this->page_template["ptmp"]=$arr_temp["ptmp"];
		$this->page_template["pctmp"]=$arr_temp["pctmp"];
		$this->arr_temp["page_str"]	= $arr_temp["page_str"]?$arr_temp["page_str"]:"page";	
		$this->row_count=$arr_temp['row_count']?$arr_temp['row_count']:0;		
		$this->init();	
			
	}
	function init(){
		$this->get_total_item_num();
			
		$this->current_page=$this->get_right_num($this->current_page);
		$this->arr_temp['start']	= $this->get_right_num($this->current_page-$this->border_length);
		$this->arr_temp['end']		= $this->get_right_num($this->arr_temp['start']+$this->border_length*2);
		$this->arr_temp['start']	= $this->get_right_num($this->arr_temp['end']-($this->border_length*2));		
		//$this->arr_temp['end']=($this->arr_temp['end']==$this->length_page && $this->arr_temp['end']-)?$this->arr_temp['end']+1:$this->arr_temp['end'];			
	}	
	function setup_page(){
			for($i=$this->arr_temp['start'];$i<=$this->arr_temp['end'];$i++){				
				$rtn_str.=($i==$this->current_page)?$this->add_current_link($i):$this->add_not_current_link($i);					
			}
			return $rtn_str;
	}
	function next_page($page=""){
	 	$nid=$this->get_right_num($this->current_page+1);
	
		return $page."?".$this->get_link($nid);
	}	
	function pre_page($page=""){
		$pid=$this->get_right_num($this->current_page-1);		
		return $page."?".$this->get_link($pid);
	}
	function page_link($page,$index){
		$pid=$this->get_right_num($index);		
		return $page."?".$this->get_link($pid);
	}
	/**
	*
	**/
	function get_link($index){
		$arr_query=$_GET;
		$arr_query[$this->arr_temp["page_str"]]=$index;
		return http_build_query($arr_query);
	}
	function build_html_link($paramName="page",$url=''){
	//print_r($this->arr_temp);
		$page_start=$this->arr_temp['start'];
		$page_end=$this->arr_temp['end'];	
		$page_current=$this->current_page;
		$limit_str=$this->get_limit_str();
		$arr_query=$_GET;
		
		$str= "<div class='page-body'><ul class='page-list' style='width:auto;'>";
			for( $i=$page_start;$i<=$page_end;$i++){
				$arr_query[$paramName]=$i;
				$link=http_build_query($arr_query);
				$url=$url?$url:$_SERVER['SCRIPT_URI'];
					
				$turl=$info['scheme']."://".$info['host'].$info['path']."?".$link;
				
				if(1){
					$url=$_SERVER['REQUEST_URI'];
					$info=(parse_url($url));	
					$turl=("http://".$_SERVER['HTTP_HOST'].($info['path'])."?".$link);
				}				
		
				$str.= "<li style='height:20px;clear:none;width:22px;margin:0px 5px;'><a class=".($i==$page_current?"current":"page-item")."  href='$turl'>{$i}</a></li>";
			}
			$str.= "</ul></div>";
		return $str;
	}
	function get_right_num($num){
		$num=($num<=0 or !$num)?1:$num;	
		$num=(!$num)?1:$num;
		//var_dump($this->length_page);
		$num=($num <= $this->length_page)?$num:$this->length_page;		
		return $num;
	}		
	function add_not_current_link($link_num){
		$template=$this->$page_template["ptmp"]?$this->$page_template["ptmp"]:"<a href='page_num.php?id={id}'>[{id}] </a> ";
		$rtn=	str_replace("{id}",$link_num,$template);
		$rtn=str_replace("{page_num}",$link_num,$rtn);
		return $rtn;
	}
	function add_current_link($link_num){	
		$template=$this->$page_template["pctmp"]?$this->$page_template["pctmp"]:"<a href=page_num.php?id=".$link_num.">[".$link_num."] </a> ";
		$rtn=	str_replace("{id}",$link_num,$template);
		$rtn=str_replace("{page_num}",$link_num,$rtn);
		return $rtn;
	}
	/**************************以上为静态独立的部分**************************/
	private function get_total_item_num(){
		global $db;
		if($this->row_count!==false){
			$this->totalItemNum = $this->row_count;		
		}else{
			$this->totalItemNum =$this->row_count;		}
	
		$total_page_num=ceil($this->totalItemNum/(($this->perpageItemNum<=0)?6:$this->perpageItemNum));	
		$total_page_num=$total_page_num?$total_page_num:1;	
		$this->length_page=$total_page_num;		
		//print_r($total_page_num);
		return $this->totalItemNum ;
	}
	/**
	*取得数据；
	**/	
	public function get_data(){
		global $db;		
		$argTableName=$this->tablename;
		$argWhere=$this->where;			
		$where	=	$argWhere ." ORDER BY id DESC ".$this->get_limit_str($this->current_page);		
		$sql_str	="SELECT * FROM ".$argTableName." WHERE ".$where;		
		$db_row=$db->row_query($sql_str);
		$this->init();
		return $db_row;
	}
	/**
	*生成limit查询字符串；
	**/
	public function get_limit_str(){		
		$limit_start=($this->current_page-1)*$this->perpageItemNum;
		$limit_start=($limit_start<0)?0:$limit_start;		
		return " limit ".$limit_start.",".$this->perpageItemNum;
	}
}

?>